#include "stdafx.h"
#include "pyhgeInputEvent.h"
#include "pyhgeCore.h"
PyTypeObject InputEventType = {
	PyObject_HEAD_INIT(NULL)
	0,                         /*ob_size*/
	"hge.InputEvent",             /*tp_name*/
	sizeof(pyInputEvent), /*tp_basicsize*/
	0,                         /*tp_itemsize*/
	0,                         /*tp_dealloc*/
	0,                         /*tp_print*/
	0,                         /*tp_getattr*/
	0,                         /*tp_setattr*/
	0,                         /*tp_compare*/
	0,                         /*tp_repr*/
	0,                         /*tp_as_number*/
	0,                         /*tp_as_sequence*/
	0,                         /*tp_as_mapping*/
	0,                         /*tp_hash */
	0,                         /*tp_call*/
	0,                         /*tp_str*/
	0,                         /*tp_getattro*/
	0,                         /*tp_setattro*/
	0,                         /*tp_as_buffer*/
	Py_TPFLAGS_DEFAULT,        /*tp_flags*/
	"Input Event structure is used with Input_GetEvent function to receive buffered input events",/* tp_doc */
};

static PyMemberDef InputEvent_members[] = {
	{"type",T_INT,offsetof(pyInputEvent,event.type),0,"Type of the event."},
	{"key",T_INT,offsetof(pyInputEvent,event.key),0,"The key code."},
	{"flags",T_INT,offsetof(pyInputEvent,event.flags),0,""},
	{"chr",T_INT,offsetof(pyInputEvent,event.chr),0,"The character code, cosidering the current keyboard states and input locale."},
	{"wheel",T_INT,offsetof(pyInputEvent,event.wheel),0,"The number of notches the mouse wheel was rotated through."},
	{"x",T_FLOAT,offsetof(pyInputEvent,event.x),0,"Mouse cursor X-position."},
	{"y",T_FLOAT,offsetof(pyInputEvent,event.y),0,"Mouse cursor Y-position."},
	{NULL}  /* Sentinel */
};
static PyObject *
pyEventInput_repr(PyObject * self)
{
	if (!PyObject_TypeCheck(self,&InputEventType))
	{
		PyErr_SetString(PyExc_TypeError,"InputEvent is required.");
		return 0;
	}
	pyInputEvent * obj = (pyInputEvent*)self;
	const char * type = NULL;
	const char * key;
	char x[25],y[25];
	sprintf(x,"%.2f",obj->event.x);
	sprintf(y,"%.2f",obj->event.y);
	//char flags[512] = {0};
	switch(obj->event.type)
	{
	case INPUT_KEYDOWN:
		type = "INPUT_KEYDOWN";
		key = hge->Input_GetKeyName(obj->event.key);
		break;
	case INPUT_KEYUP:
		type = "INPUT_KEYUP";
		key = hge->Input_GetKeyName(obj->event.key);
		break;
	case INPUT_MBUTTONDOWN:
		type = "INPUT_MBUTTONDOWN";
		key = hge->Input_GetKeyName(obj->event.key);
		break;
	case INPUT_MBUTTONUP:
		type = "INPUT_MBUTTONUP";
		key = hge->Input_GetKeyName(obj->event.key);
		break;
	case INPUT_MOUSEMOVE:
		type = "INPUT_MOUSEMOVE";
		key = "?";
		break;
	case INPUT_MOUSEWHEEL:
		type = "INPUT_MOUSEWHEEL";
		key = "?";
		break;
	default:
		type = "?";
		key = "?";
		break;
	}
	
	return PyString_FromFormat("<InputEvent type=%s key=%s wheel=%d x=%s y=%s>"
		,type,key,obj->event.wheel,x,y);
}
extern pyInputEvent * 
pyNewInputEvent()
{
	pyInputEvent * obj = (pyInputEvent*)(InputEventType.tp_alloc)(&InputEventType,0);
	return obj;
}

int 
pyInitInputEvent(PyObject * mod)
{
	InputEventType.tp_members = InputEvent_members;
	InputEventType.tp_repr = pyEventInput_repr;
	InputEventType.tp_str = pyEventInput_repr;
	if (PyType_Ready(&InputEventType) < 0) return -1;
	Py_INCREF(&InputEventType);
	return PyModule_AddObject(mod, "InputEvent", (PyObject *)&InputEventType);
}